//index.js
//获取应用实例
const MENU_WIDTH_SCALE = 0.82;
const FAST_SPEED_SECOND = 300;
const FAST_SPEED_DISTANCE = 5;
const FAST_SPEED_EFF_Y = 50;
const app = getApp()

Page({
  data: {
    ui: {
      windowWidth: 0,
      menuWidth: 0,
      offsetLeft: 0,
      tStart: true
    }
  },
  onLoad() {
    try {
      let res = wx.getSystemInfoSync() //同步版本
      this.windowWidth = res.windowWidth; //获取屏幕宽度
      this.data.ui.menuWidth = this.windowWidth * MENU_WIDTH_SCALE;
      this.data.ui.offsetLeft = 0;
      console.log("111111111" + -this.data.ui.menuWidth / 3 + this.data.ui.offsetLeft / 3);
      this.data.ui.windowWidth = res.windowWidth;
      this.setData({
        ui: this.data.ui
      })
    } catch (e) {}
  },
  //开始触发(touchstart)
  handlerStart(e) {
    let { clientX, clientY } = e.touches[0];//屏幕高宽
    this.tapStartX = clientX;
    this.tapStartY = clientY;
    this.tapStartTime = e.timeStamp;//滑动的时间
    this.startX = clientX;
    this.data.ui.tStart = true;
    this.setData({ ui: this.data.ui })
    console.log('handlerStart+++11111')
  },
  //触发中断(touchcancel)
  handlerCancel(e) {
    console.log('handlerCancel+++11111')
  },
  //手指触摸动作结束(touchend)
  handlerEnd(e) {
    this.data.ui.tStart = false;
    this.setData({ ui: this.data.ui })
    let { ui } = this.data;
    let { clientX, clientY } = e.changedTouches[0];
    console.log('handlerEnd****clientX***' + clientX)
    console.log('handlerEnd****clientY***' + clientY)
    let endTime = e.timeStamp;
    //快速滑动
    console.log("快速滑动start")
    console.log("快速滑动start**endTime" + endTime)
    console.log("快速滑动start**this.tapStartTime" + this.tapStartTime)
    if (endTime - this.tapStartTime <= FAST_SPEED_SECOND) {
      console.log("快速滑动start**clientX" + clientX)
      console.log("快速滑动start**this.tapStartX" + this.tapStartX)
      //向左
      if (this.tapStartX - clientX > FAST_SPEED_DISTANCE) {
        ui.offsetLeft = 0;
      } else if (this.tapStartX - clientX < -FAST_SPEED_DISTANCE && Math.abs(this.tapStartY - clientY) < FAST_SPEED_EFF_Y) {
        ui.offsetLeft = ui.menuWidth;
      } else {
        console.log("快速滑动ui.offsetLeft" + ui.offsetLeft)
        if (ui.offsetLeft >= ui.menuWidth / 2) {
          console.log("快速滑动start***ui.menuWidth" + ui.menuWidth)
          ui.offsetLeft = ui.menuWidth;
        } else {
          ui.offsetLeft = 0;
        }
      }
    } else {
      console.log("快速滑动2ui.offsetLeft" + ui.offsetLeft)
      console.log("快速滑动2ui.menuWidth" + ui.menuWidth / 2)
      if (ui.offsetLeft >= ui.menuWidth / 2) {
        console.log("快速滑动start2***ui.menuWidth" + ui.menuWidth)
        ui.offsetLeft = ui.menuWidth;
      } else {
        ui.offsetLeft = 0;
      }
    }
    this.setData({ ui: ui })
    console.log('handlerEnd+++11111')
  },
  //catch阻止冒泡,手指移动(touchmove)
  handlerMove(e) {

    let { clientX } = e.changedTouches[0];//获取屏幕宽度
    console.log('handlerMove+++clientX' + clientX)
    let { ui } = this.data;
    console.log("原始宽度" + this.startX )
    console.log("触摸宽度" + clientX)
    let offsetX = this.startX - clientX;
    this.startX = clientX;
    console.log("宽度" + offsetX)
    ui.offsetLeft = ui.offsetLeft- offsetX;
    console.log("计算宽度" + ui.offsetLeft)
    if (ui.offsetLeft <= 0) {
      console.log("ui.offsetLeft*****" + ui.offsetLeft )
      ui.offsetLeft = 0;
    } else if (ui.offsetLeft >= ui.menuWidth) {
      console.log("ui.offsetLeft*****" + ui.menuWidth)
      ui.offsetLeft = ui.menuWidth;
    }
    this.setData({ ui: ui })
    console.log('handlerMove+++11111')
  },
  handlerScroll(e) {
    console.log('handlerScroll+++11111')
  },
  //空白页点击
  handlerPageTap(e){
    let { ui}=this.data;
    if (ui.offsetLeft==0){
      ui.offsetLeft = ui.menuWidth;
    }else{
      ui.offsetLeft = 0;
    }
    this.setData({ ui: ui })
    console.log('handlerPageTap+++11111')
  },
  //头像点击
  handlerAvatarTap(e){
    let { ui } = this.data;
    if (ui.offsetLeft == 0) {
      ui.offsetLeft = ui.menuWidth;
    } else {
      ui.offsetLeft = 0;
    }
    this.setData({ ui: ui })
    console.log('handlerAvatarTap+++11111')
  }



})